package org.neowei.image.api.impl;

import java.io.File;

import org.neowei.common.NioFileHelper;
import org.neowei.common.exception.CommondException;
import org.neowei.image.api.ImageApi;
import org.neowei.image.api.ImageOption;
import org.neowei.image.commond.Handle.AbstractHandle;
import org.neowei.image.commond.Handle.BGResizeHandle;
import org.neowei.image.commond.Handle.CropHandle;
import org.neowei.image.commond.Handle.CropResizeHandle;
import org.neowei.image.commond.Handle.ResizeHandle;
import org.neowei.image.commond.Handle.WaterHandle;

public class ImageApiImpl implements ImageApi {
	private ResizeHandle resizeHandle = null;
	private BGResizeHandle bGResizeHandle = null;
	private CropHandle cropHandle = null;
	private WaterHandle waterHandle = null;
	private CropResizeHandle cropResizeHandle = null;
	public final long createtime;

	public ImageApiImpl() {
		createtime = System.nanoTime();
	}

	@Override
	public byte[] doResizeImage(ImageOption imageOption) throws Exception {
		return this.getPicBytes(imageOption, this.getResizeHandle(), imageOption.isIsdelete());
	}

	@Override
	public byte[] doBGResizeImage(ImageOption imageOption) throws Exception {
		return this.getPicBytes(imageOption, this.getbGResizeHandle(), imageOption.isIsdelete());
	}

	@Override
	public byte[] doCropImage(ImageOption imageOption) throws Exception {
		return this.getPicBytes(imageOption, this.getCropHandle(), imageOption.isIsdelete());
	}

	@Override
	public byte[] doWaterImage(ImageOption imageOption) throws Exception {
		return this.getPicBytes(imageOption, this.getWaterHandle(), imageOption.isIsdelete());
	}

	@Override
	public byte[] doCropResizeImage(ImageOption imageOption) throws Exception {
		return this.getPicBytes(imageOption, this.getCropResizeHandle(), imageOption.isIsdelete());
	}

	private byte[] getPicBytes(ImageOption imageOption, AbstractHandle crop, boolean isdelete) {
		try {
			crop.setImageOption(imageOption);
			File file = new File(crop.getDespathWithFilename());
			if (!file.exists()) {
				crop.doHandle(imageOption);
			}
			byte[] bytes = NioFileHelper.getByte(file);
			if (isdelete) {
				file.delete();
			}
			return bytes;
		} catch (Exception e) {
			e.printStackTrace();
			throw new CommondException(e);
		}
	}

	private ResizeHandle getResizeHandle() {
		return this.resizeHandle == null ? this.resizeHandle = new ResizeHandle() : this.resizeHandle;
	}

	private CropResizeHandle getCropResizeHandle() {
		return this.cropResizeHandle == null ? this.cropResizeHandle = new CropResizeHandle() : this.cropResizeHandle;
	}

	private BGResizeHandle getbGResizeHandle() {
		return this.bGResizeHandle == null ? this.bGResizeHandle = new BGResizeHandle() : this.bGResizeHandle;
	}

	private CropHandle getCropHandle() {
		return this.cropHandle == null ? this.cropHandle = new CropHandle() : this.cropHandle;
	}

	private WaterHandle getWaterHandle() {
		return this.waterHandle == null ? this.waterHandle = new WaterHandle() : this.waterHandle;
	}

	@Override
	public int compareTo(ImageApiImpl o) {
		return (int) (o.createtime - this.createtime);
	}

}
